/**
 * Created by TomSean on 2017/6/21.
 */
$.fn.extend({
    lonixDownList : function (option) {
        var downList = new LonixDownList($(this));
        if (undefined != option.source) {
            var value = $(this).html();
            downList.buildFrame();
            downList.deployData(option.source, option.title, option.value, value, option.selectFunction);
        } else {
            downList.bindEvent();
        }
        return downList;
    }
});
var LonixDownList = function ($) {
    this.$ = $;
}
LonixDownList.prototype = {
    buildFrame : function () {
        this.$.addClass('lonix');
        this.$.addClass('downlist');
        var downlistHtml = '<span class="title"></span>';
        downlistHtml += '<div class="list">';
        downlistHtml += '<ul>';
        downlistHtml += '</ul>';
        downlistHtml += '</div>';
        this.$.html(downlistHtml);
    },
    deployData : function (list, title, value, selectedValue, selectFunction) {
        title = title || 'title';
        value = value || 'value';
        if (isEmpty(list)) {
            console.log('[downlist]数据为空');
            return;
        }
        var liHtml = '';
        for (var i = 0; i < list.length; i++) {
            var li = list[i];
            liHtml += '<li>';
            var selected = li.selected ? 'selected' : '';
            if (selectedValue == li[value]) {
                selected = 'selected';
                this.$.find('.title').html(li[title]);
                this.$.find('.title').attr('data-value', li[value]);
            }
            liHtml += '<a class="'+selected+'" data-value="'+li[value]+'">'+li[title]+'</a>';
            liHtml += '</li>';
        }
        this.$.find('ul').html(liHtml);
        if (this.$.find('a.selected').length == 0) {
            this.$.find('a:eq(1)').click();
        }
        this.bindEvent(selectFunction);
    },
    bindEvent : function (selectFunction) {
        var parentThis = this;
        // 绑定下拉事件
        this.$.find('.title').bind('click', function(e) {
            $(this).next().find('ul').show();
            $(this).css({background:'#cccccc'});
            e.stopPropagation();
        });
        this.$.find('.list').bind('click', function(e) {
            e.stopPropagation();
        });
        // 点击事件绑定
        this.$.find('a').bind('click', function() {
            var txt = $(this).html(), val = $(this).attr('data-value');
            if (undefined == $(this).attr('data-value')) {
                txt = $(this).html();
                val = txt;
            }
            parentThis.$.find('.title').html(txt);
            parentThis.$.find('.title').attr('data-value', val);
            parentThis.$.find('.title').css({background:'transparent'});
            parentThis.$.find('.list ul').hide();
            $(this).addClass('selected');
            $(this).parent().siblings().find('a').removeClass('selected');
            if (!isEmpty(selectFunction) && typeof(selectFunction)==='function') {
                selectFunction(parentThis.val());
            }
        });
        $(document).bind('click', function() {
            $('.title').css({background:'transparent'});
            $('.lonix.downlist .list ul').hide();
        });
    },
    val : function () {
        return this.$.find('.title').attr('data-value');
    }
};